package com.awesome.servicedemo.aidl;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;

import com.awesome.aidldemo.AidlService;
import com.awesome.aidldemo.IDownloadManager;
import com.awesome.aidldemo.IMyBinder;
import com.awesome.aidldemo.OnDownloadListener;
import com.awesome.aidldemo.bean.DownloadBean;
import com.awesome.common.util.log.ShowLogUtil;
import com.awesome.servicedemo.R;

public class AidlActivity extends AppCompatActivity {
    ServiceConnection conn1 = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            ShowLogUtil.info("onServiceConnected");
            IMyBinder iMyBinder = IMyBinder.Stub.asInterface(service);
            try {
                IDownloadManager downloadManager = iMyBinder.getDownloadManager();
                if (downloadManager != null) {
                    DownloadBean downloadBean = downloadManager.getDownloadBean();
                    ShowLogUtil.info("客户端获取数据 downloadBean.getFileName()=" + downloadBean.getFileName());
                    ShowLogUtil.info("客户端获取数据 downloadBean.getFileSize()=" + downloadBean.getFileSize());
                }
                iMyBinder.registerDownloadListener(mOnDownloadListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            ShowLogUtil.info("onServiceDisconnected");
        }
    };

    OnDownloadListener mOnDownloadListener = new OnDownloadListener.Stub() {
        @Override
        public void download(DownloadBean downloadBean) throws RemoteException {
            ShowLogUtil.info("客户端获取数据 downloadBean.getFileName()=" + downloadBean.getFileName());
            ShowLogUtil.info("客户端获取数据 downloadBean.getFileSize()=" + downloadBean.getFileSize());
        }

        @Override
        public void onFileCount(int fileCount) throws RemoteException {
            ShowLogUtil.info("客户端获取数据 fileCount=" + fileCount);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidl);
        findViewById(R.id.btn_start_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //远程启动服务
                Intent intent = new Intent(AidlActivity.this, AidlService.class);
                startService(intent);
            }
        });
        findViewById(R.id.btn_stop_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AidlActivity.this, AidlService.class);
                stopService(intent);
            }
        });
        findViewById(R.id.bind_service_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bind(conn1);
            }
        });
        findViewById(R.id.unbind_service_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unBind(conn1);
            }
        });
    }

    public void bind(ServiceConnection conn){
        Intent intent = new Intent(AidlActivity.this, AidlService.class);
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    public void unBind(ServiceConnection conn){
        try {
            unbindService(conn);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}